package sz.com.postoffice.jxyzwgcase.controller;

import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sz.com.postoffice.jxyzwgcase.core.CommonResult;
import sz.com.postoffice.jxyzwgcase.pojo.UserAdo;
import sz.com.postoffice.jxyzwgcase.service.ForecastIncomeService;
import sz.com.postoffice.jxyzwgcase.service.UserAdoService;


/**
 * 描述:
 *
 * @author baiyu, du
 * @date 2020/11/27 14:51
 */
@Api(value = "月收入统计", tags = "{月收入统计}")
@RestController
@Slf4j
@RequestMapping(value = "/forecastIncome")
public class ForecastIncomeController {
    @Autowired
    private UserAdoService userAdoService;
    @Autowired
    private ForecastIncomeService forecastIncomeService;

    @ApiOperation(value = "用户查询薪酬登记", notes = "用户查询薪酬登记，轮询频率5s")
    @GetMapping("/leave/{adoId}/{beginTime}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adoId", value = "用户adoId", dataType = "String", paramType = "path", required = true),
            @ApiImplicitParam(name = "beginTime", value = "本次初始时间毫秒值，精确到毫秒", dataType = "Long", paramType = "path", required = true)
    })
    public CommonResult leaveRegister(@PathVariable("adoId") String adoId, @PathVariable("beginTime") Long beginTime) {
        CommonResult result = new CommonResult();

        if (org.springframework.util.StringUtils.isEmpty(adoId) || org.springframework.util.StringUtils.isEmpty(beginTime)) {
            throw new RuntimeException("传入数据有误");
        }

        UserAdo searchAdo = getUserAdo(adoId);

        if (searchAdo == null) {
            result.setMsg("该用户信息未录入系统");
            result.setCode(400);
            log.info("该用户信息未录入系统 adoId:"+adoId);
            return result;
        }
        log.info("adoId找到对应用户，userCode: " + searchAdo.getUserCode());
        log.info("userName: " + searchAdo.getUserName());
        if (StringUtils.isEmpty(searchAdo.getUserCode())) {
            result.setMsg("该用户信息未录入系统");
            result.setCode(400);
            log.info("该用户信息未录入系统 adoId:"+adoId);
            return result;
        }

        forecastIncomeService.addAccessRecord(searchAdo.getUserCode(), beginTime);
        result.setMsg("登记成功");
        result.setCode(0);

        return result;
    }

    private UserAdo getUserAdo(String adoId) {
        UserAdo userAdo = new UserAdo();
        userAdo.setAdoId(adoId);
        return userAdoService.findByEntity(userAdo);
    }


}



